package idv.cjcat.stardust.common.particles;
	
	/**
	 * Array implementatoin of particle collection, with splicing operation even faster than linked-lists.
	 */
	public class ParticleFastArray implements ParticleCollection {
		
		/** @private */
		static final int DEFAULT_ARRAY_SIZE = 32;
		/** @private */
		Particle[] internalArray = new Particle[DEFAULT_ARRAY_SIZE];
		/** @private */
		int index = 0; //points at the first null element
		
		public boolean shrinkInternalArrayOnClear;
		
		public ParticleFastArray(boolean shrinkInternalArrayOnClear) {
			this.shrinkInternalArrayOnClear = shrinkInternalArrayOnClear;
		}
		public ParticleFastArray() {
			this.shrinkInternalArrayOnClear = true;
		}
		
		/**
		 * @inheritDoc
		 */
		public final void add(Particle particle) {
			internalArray[index++] = particle;
			
			if (index == (internalArray.length)) {
				Particle[] newArray = new Particle[internalArray.length*2];
				for (int i=0; i<internalArray.length; i++)
					newArray[i]=internalArray[i];
				internalArray = newArray;
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public final ParticleIterator getIterator() {
			return new ParticleFastArrayIterator(this);
		}
		
		/**
		 * @inheritDoc
		 */
		public final void sort() {
			internalArray.sortOn("x", Array.NUMERIC);
		}
		
		/**
		 * @inheritDoc
		 */
		public final int getSize() {
			return index;
		}
		
		/**
		 * @inheritDoc
		 */
		public final void clear() {
			for (int i = 0; i < internalArray.length; i++) internalArray[i] = null;
			if (shrinkInternalArrayOnClear) internalArray = new Particle[DEFAULT_ARRAY_SIZE];
			index = 0;
		}
	}
